package com.hu.boot.controller;

import com.hu.boot.entity.User;
import com.hu.boot.interceptors.Interceptor1;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/users")
public class RestApiController {

    // 声明一个日志对象
    private static final Logger log = LoggerFactory.getLogger(RestApiController.class);

    /*
        ResponseEntity 是 SpringMVC 封装的一个专用于 restful 的响应类,这个类可以在响应时提供响应状态码,同时还可以自定义响应头信息
        HttpStatus 是 SpringMVC 封装的一个枚举类型,这个类中都是网络中的状态码
     */
    @GetMapping("/{id}")
    public ResponseEntity<User> user(@PathVariable("id") Integer id) {
        log.info("id: " + id);
        User user = new User(id, "小红", new Date(), 12.0);
        return new ResponseEntity<>(user, HttpStatus.OK);
    }

    @GetMapping
    public ResponseEntity<List<User>> users() {
        ArrayList<User> users = new ArrayList<>();
        users.add(new User(21, "小红", new Date(), 12.0));
        users.add(new User(24, "小明", new Date(), 12.0));
        return new ResponseEntity<>(users, HttpStatus.OK);
    }


    @PostMapping
    public ResponseEntity<Void> save(@RequestBody User user) {
        return new ResponseEntity<>(HttpStatus.NO_CONTENT); //没有内容
    }

    @PutMapping("/{id}")
    public ResponseEntity<Void> update(@PathVariable("id") Integer id, @RequestBody User user) {
        log.info("id:{} ", id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT); //没有内容
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable("id") Integer id) {
        log.info("本次id: {}", id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT); //没有内容
    }
}
